﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Main
{
    public partial class _Default : System.Web.UI.Page
    {
        /// <summary>
        /// sets visibility of Administration functions according to login information, if Admin-show, if not Admin-hide
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                if (Session["LIMITEDUSER"] == null)
                {
                    TableCell3.Visible = true;
                    TableCell4.Visible = true;
                    TableCell5.Visible = true;
                }
                else if (Session["LIMITEDUSER"] == "true")
                {
                    TableCell3.Visible = false;
                    TableCell4.Visible = false;
                    TableCell5.Visible = false;
                    Administration.Visible = false;
                }

            }
            else
                Response.Redirect("Login.aspx");
            
            
            ControlLiveIDs.Control control = new ControlLiveIDs.Control();
            string fullDomain = ListExistingLiveIDs(control.ViewLiveID(Session["Company"].ToString()));

            TableCell6.Text = @"<a href=""http://outlook.com/owa/" + fullDomain + @""">Mailbox</a>";

            string exactDomain = fullDomain.Substring(fullDomain.IndexOf('@') + 1);
            if (exactDomain.Contains(".onmicrosoft.com"))
                exactDomain = exactDomain.Substring(0, exactDomain.IndexOf('.'));

            TableCell7.Text = @"<a href=""http://" + exactDomain + @".sharepoint.com/teamsite"">Team Site</a>";
        }

        /// <summary>
        /// returns first half of the address of Office 365-enabled Windows Live ID to set URLs for Mailbox and Portal
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private string ListExistingLiveIDs(List<WindowsLiveID> list)
        {
            string exactDomain = String.Empty;
            
            if(list.Count > 0)
            {
                foreach (var Result in list)
                {
                    exactDomain = Result.WL_ID;
                    exactDomain = exactDomain.Substring(exactDomain.IndexOf('@') + 1);
                }
            }
            return exactDomain;
        }

        /// <summary>
        /// logs out the current user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Session.RemoveAll();
            Session.Abandon();
            Response.Redirect("Login.aspx");
        }
    }
}
